package com.qushiyun.devops.admin.sync.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.qushiyun.devops.admin.sync.data.po.ConditionConfig;
import com.qushiyun.devops.admin.sync.data.service.ConditionConfigService;
import com.qushiyun.devops.common.core.web.response.ResponseResult;
import com.qushiyun.devops.common.framework.mybatis.page.vo.PageVO;
import com.qushiyun.devops.common.log.annotation.Log;
import com.qushiyun.devops.common.log.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ConditionConfig
 *
 * @author zhouzx
 */
@RestController
@RequestMapping("/sync/condition")
public class ConditionConfigController {

    @Autowired
    private ConditionConfigService ConditionConfigService;

    @GetMapping("/page")
    @PreAuthorize("@auth.hasUrl()")
    public ResponseResult<PageVO<ConditionConfig>> page(PageDTO pageDTO, ConditionConfig config) {
        PageVO<ConditionConfig> pageVO = ConditionConfigService.pageList(pageDTO, config);
        return ResponseResult.success(pageVO);
    }

    @GetMapping("/list")
    @PreAuthorize("@auth.hasUrl()")
    public ResponseResult<List<ConditionConfig>> list() {
        LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(ConditionConfig.class).select(ConditionConfig::getId, ConditionConfig::getName);
        List<ConditionConfig> list = ConditionConfigService.list(wrapper);
        return ResponseResult.success(list);
    }

    @GetMapping("/getById/{id}")
    @PreAuthorize("@auth.hasUrl()")
    public ResponseResult<ConditionConfig> getById(@PathVariable Long id) {
        return ResponseResult.success(ConditionConfigService.getById(id));
    }

    @PostMapping
    @PreAuthorize("@auth.hasUrl()")
    @Log(moduleName = "同步条件", businessType = BusinessType.ADD)
    public ResponseResult add(@Valid @RequestBody ConditionConfig config) {
        ConditionConfigService.save(config);
        return ResponseResult.success();
    }

    @PutMapping
    @ApiOperation("修改")
    @PreAuthorize("@auth.hasUrl()")
    @Log(moduleName = "同步条件", businessType = BusinessType.UPDATE)
    public ResponseResult update(@Valid @RequestBody ConditionConfig updateDTO) {
        ConditionConfigService.updateById(updateDTO);
        return ResponseResult.success();
    }

    @DeleteMapping("/delete/{ids}")
    @ApiOperation("删除")
    @PreAuthorize("@auth.hasUrl()")
    @Log(moduleName = "同步条件", businessType = BusinessType.DELETE)
    public ResponseResult delete(@PathVariable Long[] ids) {
        ConditionConfigService.removeByIds(Arrays.asList(ids));
        return ResponseResult.success();
    }


}
